<?php
/**
 * 自由购任务
 */
class FreeBuyAction extends Action {
	private function getOrderDetailPrice($oid,$customerId){
		Session::switchCustomer(0);
		$DetailModel = M("module_product_order_detail");
		$OrderModel = M("module_product_order");
		Session::switchCustomer($customerId);
		$details=$DetailModel->where("oid=$oid")->select();
		$order=$OrderModel->where("id=$oid")->find();
		$sum=0;

		foreach ($details as $key => $value) {
			$sum+=$value["price"]*$value["count"];
		}
		if($order && $order["logisticsCost"]) {
			$sum+=$order["logisticsCost"];
		}
		return $sum;
	}
	/**
	 * [sendFreeBuyOrderNotice 用户订单提醒]
	 * @return [type] [description]
	 */
	public function sendFreeBuyOrderNotice(){
		Session::switchCustomer(0);
		$OrderModel=M("module_product_order");
		$FreeByModel=M("module_freebuy_record");
		$time =time();
		$str="select * from wx_module_product_order where status='paypart'";
		$orderList=$OrderModel->query($str);
		$time=time();
		if(count($orderList) >0 ) {
			$customerId=$orderList[0]["fromCustomerId"];
		}
		Session::switchCustomer($customerId);
		$_GET["customerId"]=$customerId;
		foreach ($orderList as $key => $value) {
			$toUser=$value["fromUserName"];
			$createTime=strtotime($value["createTime"]);
			$deTime=$time-$createTime;
			$hour=$deTime/3600;
			$oid=$value["id"];
			$refueMoney=$value["refueMoney"];
			$price=self::getOrderDetailPrice($oid,$customerId);
			$deposit=$value["price"];
			//需要补齐的金额
			$chPrice=round(($price-$deposit-$refueMoney),2);
			$url = C("SERVER_HOST") . "call.html?customerId=" . $customerId .
			"&module=freebuy&method=getView&params=%7B%22oid%22%3A%22$oid%22%7D";
			if($hour <= 48) {
				if($hour <= 24) {
					$detail="<a href='".$url."'>点我前往活动</a>";
					$count=$FreeByModel->where("oid=$oid")->count();
					$text="嗨，找到你了，你参加的“我要代言”活动，还须邀请".ceil($chPrice)."人支持就完成获得奖品啦。加油！（秘诀：截图然后群发好友效果最好）\n".$detail;
				}else {
					$detail="<a href='".$url."'>点击这里前往</a>";
					$text="嗨，又找到你了，说好的为我们代言呢？还是有其他想法？好吧，你可以继续邀请、补齐差价或者退款。".$detail;
				}
				$result=WeixinApi::sendServiceMsg($toUser, 'text',$text);
			}
			if($hour > 168) { //超过15天退款
				$refundMoney=$value["price"];
				$out=R('Product/orderRefund', array($oid,$refundMoney,"111111"));
			}
		}
	}
}